<?php

namespace Biblibase\SiteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;

use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Image
 *
 * @ORM\Table(name="image")
 * @ORM\Entity(repositoryClass="Biblibase\SiteBundle\Entity\ImageRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Image
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="src", type="string", length=255)
     */
    private $src;
	
	  /**
     * @var Symfony\Component\HttpFoundation\File\UploadedFile
     * @Assert\Image()
     */
	 private $file;
	 
	 private $tempFilename;
	 
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }


    /**
     * Set src
     *
     * @param string $src
     * @return Image
     */
    public function setSrc($src)
    {
        $this->src = $src;
    
        return $this;
    }

    /**
     * Get src
     *
     * @return string 
     */
    public function getSrc()
    {
        return $this->src;
    }

	 /**
     * Set tempFilename
     *
     * @param string $tmp
     * @return Image
     */
     public function setTempFilename($tmp = null)
     {
        $this->tempFilename = $tmp ;
    
        return $this;
     }
	 
	 /**
     * Get file
     *
     * @return Symfony\Component\HttpFoundation\File\UploadedFile 
     */
	 public function getFile()
	 {
		  return $this->file;	 
	 }
	 
	 /**
     * Set file
     *
     * @param Symfony\Component\HttpFoundation\File\UploadedFile $file
     */
	 public function setFile(UploadedFile $file)
  	 {
    	 $this->file = $file;
 
    	 // On vérifie si on avait déjà un fichier pour cette entité
    	if (null !== $this->src) {
       	// On sauvegarde l'extension du fichier pour le supprimer plus tard
      	$this->tempFilename = $this->src;
 
      	$this->src = null;
    	}
  	 }
 
  	/**
   * @ORM\PrePersist()
   * @ORM\PreUpdate()
   */
  	public function preUpload()
  	{
    	 // Si jamais il n'y a pas de fichier (champ facultatif)
    	if (null === $this->file) {
      	return;
    	}
 
    	  // Le nom du fichier est son id, on doit juste stocker également son extension
    	$this->src = $this->file->guessExtension();
  }
 
  /**
   * @ORM\PostPersist()
   * @ORM\PostUpdate()
   */
  	public function upload()
  	{
    	
    	if (null === $this->file) {
    	  return;
    	}
 
    	
    	if (null !== $this->tempFilename) {
      	$oldFile = $this->getUploadRootDir().'/'.$this->id.'.'.$this->tempFilename;
      	if (file_exists($oldFile)) {
        		unlink($oldFile);
      	}
    	}
 		
    	$this->file->move(
      	$this->getUploadRootDir(),
      	$this->id.'.'.$this->src  
    	);
  }
 
  /**
   * @ORM\PreRemove()
   */
  public function preRemoveUpload()
  {
    	
    	$this->tempFilename = $this->getUploadRootDir().'/'.$this->id.'.'.$this->src;
  }
 
  /**
   * @ORM\PostRemove()
   */
  public function removeUpload()
  {
    	
    	if (file_exists($this->tempFilename)) {
      	
      	unlink($this->tempFilename);
    	}
  }
 
  	public function getUploadDir()
  	{
  	 	
    	return 'uploads/img';
  	}
 
  	public function getUploadRootDir()
  	{
    	return __DIR__.'/../../../../web/'.$this->getUploadDir();
  	}
}